package cn.zzszxyy.hr.repository;

import cn.zzszxyy.hr.model.MenuEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-27
 * 说明:菜单管理
 */
@Repository
public interface MenuRepository extends JpaRepository<MenuEntity, Integer>, JpaSpecificationExecutor<MenuEntity> {

    public final static String FIND_MemusByRole_QUERY = "SELECT m FROM MenuEntity m " +
            "where m.menuId in (select rm.menuId from RoleMenuEntity rm where rm.roleId=?1) order by m.menuOrder";

    public final static String FIND_MemusByUser_QUERY = "SELECT m FROM MenuEntity m " +
            "where m.menuId in (select rm.menuId from RoleMenuEntity rm " +
            "where rm.roleId in (select u.role.roleId from UserEntity u where u.userId = ?1))";

    Page<MenuEntity> findByMenuNameLike(String menuName, Pageable pageable);

    // 查询角色相关菜单
    @Query(FIND_MemusByRole_QUERY)
    List<MenuEntity> findMenusByRoleId(int roleId);

    // 查询用户相关菜单
    @Query(FIND_MemusByUser_QUERY)
    List<MenuEntity> findMenusByUserId(int userId);
}
